package com.wing.unit.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.unit.dao.AgentTypeDao;
import com.wing.unit.model.entity.AgentType;
import com.wing.unit.model.entity.SupplierType;
import com.wing.unit.model.request.AgentTypeForm;
import com.wing.unit.model.response.AgentTypeVO;
import com.wing.unit.model.response.SupplierTypeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 代理商类别-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class AgentTypeServiceImpl extends ServiceImpl<AgentTypeDao, AgentType> implements AgentTypeService {
    private long ROOT_ID=-1l;
    @Override
    public int add(AgentTypeForm agentTypeForm) {

        //校验

        AgentType agentType =new AgentType();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(AgentType.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentTypeForm, agentType, copyOptions);

        return baseMapper.insert(agentType);
    }

    @Override
    public List<AgentTypeVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<AgentTypeVO> pageList4app(Page page, Map<String, Object> params) {
        List<AgentTypeVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public AgentTypeVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }


    @Override
    public int enable(Long id) {
        AgentTypeVO agentTypeVO=baseMapper.get4appById(id);
        if (agentTypeVO!=null){
            AgentType agentType =new AgentType();
            agentType.setId(id);
            agentType.setIsDeleted(true);
            int updateCount=baseMapper.updateById(agentType);
            if (agentTypeVO.getParentId()!=ROOT_ID){
                AgentType agentType1=new AgentType();
                agentType1.setId(agentTypeVO.getParentId());
                agentType1.setHasEnableChild(true);
                baseMapper.updateById(agentType1);
            }
            return updateCount;
        }
        return 0;
    }

    @Override
    public int disable(Long id) {
        AgentTypeVO agentTypeVO=baseMapper.get4appById(id);
        if (agentTypeVO!=null){
            AgentType agentType =new AgentType();
            agentType.setId(id);
            agentType.setIsDeleted(false);
            int updateCount=baseMapper.updateById(agentType);
            if (agentTypeVO.getParentId()!=ROOT_ID){
                int count=baseMapper.selectCount(new QueryWrapper<AgentType>().eq("parent_id",agentTypeVO.getParentId()).eq("is_deleted",true));
                if (count==0){
                    AgentType agentType1=new AgentType();
                    agentType1.setId(agentTypeVO.getParentId());
                    agentType1.setHasEnableChild(false);
                    baseMapper.updateById(agentType1);
                }
            }

            return updateCount;
        }
        return 0;
    }
}
